package wyp.library.ui.tableview;

import java.util.ArrayList;

import wyp.lib.java.tools.WypStringTool;
import wyp.library.R;
import wyp.library.ui.tableview.NSIndexPath.RowType;
import wyp.library.ui.tableview.UITableViewCell.UITableViewCellSelectionStyle;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class UITableView  {

	public UITableViewDataSource dataSource;
	public UITableViewDelegate delegate;
	private ArrayList<NSIndexPath> indexPathArray = new ArrayList<NSIndexPath>();
	protected ListView listView;
	protected Context context;

	public UITableView(Context __context, ListView __listView) {
		super();
		this.context = __context;
		this.listView = __listView;
		this.setupInit(__context);
	}

//	public UITableView(Context __context, AttributeSet attrs) {
//		super(__context, attrs);
//		this.setupInit(__context);
//	}
//
//	public UITableView(Context __context, AttributeSet attrs, int defStyle) {
//		super(__context, attrs, defStyle);
//		this.setupInit(__context);
//	}

	protected void setupInit(Context __context) {
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (delegate == null) {
					return;
				}
				delegate.didSelectRowAtIndexPath(indexPathArray.get(position));
			}
		});
	}

	public void setAdapter(ListAdapter adapter) {
		indexPathArray.clear();
		int __sectionNum = dataSource.numberOfSectionsInTableView(UITableView.this);
		__sectionNum = __sectionNum > 0 ? __sectionNum : 1;
		for (int i = 0; i < __sectionNum; i++) {
			indexPathArray.add(new NSIndexPath(i, -1, RowType.HEADER));
			int __rowNum = dataSource.numberOfRowsInSection(UITableView.this, i);
			__rowNum = __rowNum >= 0 ? __rowNum : 0;
			for (int j = 0; j < __rowNum; j++) {
				indexPathArray.add(new NSIndexPath(i, j, RowType.NORMAL));
			}

			if (WypStringTool.isNotEmpty(dataSource.titleForFooterInSection(this, i))) {
				indexPathArray.add(new NSIndexPath(i, -1, RowType.FOOTER));
			}
		}
		listView.setAdapter(adapter);
	}

	public void reloadData() {

		if (dataSource == null) {
			return;
		}

		this.setAdapter(new UITableViewAdapter());
	}

	protected View setupHeaderView(int __sectoinIndex) {
		String __header = this.dataSource.titleForHeaderInSection(this, __sectoinIndex);
		if (__header != null) {
			View __returnView = LayoutInflater.from(context).inflate(R.layout.ui_tableview_header_footer_default, null);
			TextView __headView = (TextView) __returnView.findViewById(R.id.textview);
			__headView.setText(__header);
			return __returnView;
		} else {
			return createEmptyHeaderFooterView();
		}
	}

	protected View setupFooterView(int __sectoinIndex) {
		String __footer = this.dataSource.titleForFooterInSection(this, __sectoinIndex);
		if (__footer != null) {
			View __returnView = LayoutInflater.from(context).inflate(R.layout.ui_tableview_header_footer_default, null);
			((TextView) __returnView.findViewById(R.id.textview)).setText(__footer);
			return __returnView;
		} else {
			return createEmptyHeaderFooterView();
		}
	}

	private View createEmptyHeaderFooterView() {
		View __emptyView = new View(context);
		__emptyView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 0));
		__emptyView.setBackgroundColor(Color.TRANSPARENT);
		return __emptyView;
	}

	private class UITableViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return indexPathArray.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			if (indexPathArray.get(position).rowType == RowType.NORMAL) {
				return indexPathArray.get(position).selectionStyle != UITableViewCellSelectionStyle.UITableViewCellSelectionStyleNone;
			} else {
				return false;
			}
		}

		@Override
		public View getView(int position, View __convertView, ViewGroup parent) {
			NSIndexPath __indexPath = indexPathArray.get(position);
			View __returnView = null;
			if (__indexPath.rowType == RowType.NORMAL) {
				UITableViewCell __cell = dataSource.cellForRowAtIndexPath(UITableView.this, __indexPath, __convertView);
				__cell.layoutSubviews();
				__indexPath.selectionStyle = __cell.selectionStyle;
				__returnView = __cell;
			} else if (__indexPath.rowType == RowType.HEADER) {
				__returnView = setupHeaderView(__indexPath.section);
			} else if (__indexPath.rowType == RowType.FOOTER) {
				__returnView = setupFooterView(__indexPath.section);
			}
			return __returnView;
		}

	}

}
